﻿#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading.Tasks;
using System.Windows;
using Amarok.Agents;
using NUnit.Framework;


namespace Amarok.Presentation
{
	/// <summary>
	/// </summary>
	[TestFixture]
	public class Test_ControllerFactoryExtensions
	{
		private class StubEnvironment : AgentEnvironment
		{
		}

		private class StubOptions : AgentOptions
		{
		}

		private class StubView : IView
		{
			public Object ViewModel
			{
				get
				{
					throw new NotImplementedException();
				}
				set
				{
				}
			}

			public FrameworkElement Control
			{
				get
				{
					throw new NotImplementedException();
				}
			}

			public void Invoke(Action action)
			{
				throw new NotImplementedException();
			}

			public Task InvokeAsync(Action action)
			{
				throw new NotImplementedException();
			}

			public void Send(ViewAction viewAction)
			{
				throw new NotImplementedException();
			}

			public Task SendAsync(ViewAction viewAction)
			{
				throw new NotImplementedException();
			}
		}

		private class StubViewModel : ViewModel
		{
		}

		private class StubController : Controller<StubViewModel>
		{
			public StubController(AgentEnvironment environment, AgentOptions options, IView view, StubViewModel viewModel)
				: base(environment, options, view, viewModel)
			{
			}
		}

		private class StubControllerMEA : Controller<StubViewModel, StubEnvironment, StubOptions>
		{
			public StubControllerMEA(StubEnvironment environment, StubOptions options, IView view, StubViewModel viewModel)
				: base(environment, options, view, viewModel)
			{
			}
		}

		private class MockFactory : IControllerFactory
		{
			public Type ControllerType;
			public IView View;
			public Object ViewModel;
			public AgentEnvironment Environment;
			public AgentOptions Options;
			public Int32 Called;
			public IController ReturnValue;


			public IController SpawnController(
				Type controllerType,
				IView view,
				Object viewModel,
				AgentEnvironment environment,
				AgentOptions options)
			{
				this.ControllerType = controllerType;
				this.View = view;
				this.ViewModel = viewModel;
				this.Environment = environment;
				this.Options = options;

				Called++;

				ReturnValue = new StubController(environment, options, view, viewModel as StubViewModel);

				return ReturnValue;
			}
		}


		#region SpawnController(Type, IView, Object, AgentEnvironment)

		[Test]
		public void Test_SpawnController_Type_View_ViewModel_Environment()
		{
			var factory = new MockFactory();
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();

			var controller = factory.SpawnController(
				typeof(StubController),
				view,
				viewModel,
				environment);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.ControllerType, Is.EqualTo(typeof(StubController)));
			Assert.That(factory.View, Is.SameAs(view));
			Assert.That(factory.ViewModel, Is.SameAs(viewModel));
			Assert.That(factory.Environment, Is.SameAs(environment));
			Assert.That(factory.Options, Is.SameAs(AgentOptions.Empty));
			Assert.That(factory.ReturnValue, Is.SameAs(controller));
		}

		#endregion

		#region SpawnController<T, M>(IView, AgentEnvironment, AgentOptions)

		[Test]
		public void Test_SpawnController_GenericType_View_Environment_Options()
		{
			var factory = new MockFactory();
			var view = new StubView();
			var environment = new StubEnvironment();
			var options = new StubOptions();

			var controller = factory.SpawnController<StubController, StubViewModel>(
				view,
				environment,
				options);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.ControllerType, Is.EqualTo(typeof(StubController)));
			Assert.That(factory.View, Is.SameAs(view));
			Assert.That(factory.ViewModel, Is.TypeOf<StubViewModel>());
			Assert.That(factory.Environment, Is.SameAs(environment));
			Assert.That(factory.Options, Is.SameAs(options));
			Assert.That(factory.ReturnValue, Is.SameAs(controller));
		}

		#endregion

		#region SpawnController<T, M>(IView, AgentEnvironment)

		[Test]
		public void Test_SpawnController_GenericType_View_Environment()
		{
			var factory = new MockFactory();
			var view = new StubView();
			var environment = new StubEnvironment();

			var controller = factory.SpawnController<StubController, StubViewModel>(
				view,
				environment);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.ControllerType, Is.EqualTo(typeof(StubController)));
			Assert.That(factory.View, Is.SameAs(view));
			Assert.That(factory.ViewModel, Is.TypeOf<StubViewModel>());
			Assert.That(factory.Environment, Is.SameAs(environment));
			Assert.That(factory.Options, Is.SameAs(AgentOptions.Empty));
			Assert.That(factory.ReturnValue, Is.SameAs(controller));
		}

		#endregion

		#region SpawnController<T, M>(IView, Object, AgentEnvironment, AgentOptions)

		[Test]
		public void Test_SpawnController_GenericType_View_ViewModel_Environment_Options()
		{
			var factory = new MockFactory();
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();
			var options = new StubOptions();

			var controller = factory.SpawnController<StubController, StubViewModel>(
				view,
				viewModel,
				environment,
				options);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.ControllerType, Is.EqualTo(typeof(StubController)));
			Assert.That(factory.View, Is.SameAs(view));
			Assert.That(factory.ViewModel, Is.SameAs(viewModel));
			Assert.That(factory.Environment, Is.SameAs(environment));
			Assert.That(factory.Options, Is.SameAs(options));
			Assert.That(factory.ReturnValue, Is.SameAs(controller));
		}

		#endregion

		#region SpawnController<T, M>(IView, Object, AgentEnvironment)

		[Test]
		public void Test_SpawnController_GenericType_View_ViewModel_Environment()
		{
			var factory = new MockFactory();
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();

			var controller = factory.SpawnController<StubController, StubViewModel>(
				view,
				viewModel,
				environment);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.ControllerType, Is.EqualTo(typeof(StubController)));
			Assert.That(factory.View, Is.SameAs(view));
			Assert.That(factory.ViewModel, Is.SameAs(viewModel));
			Assert.That(factory.Environment, Is.SameAs(environment));
			Assert.That(factory.Options, Is.SameAs(AgentOptions.Empty));
			Assert.That(factory.ReturnValue, Is.SameAs(controller));
		}

		#endregion

		#region SpawnController<T, M, E, O>(IView, AgentEnvrionment, AgentOptions)

		[Test]
		public void Test_SpawnController_GenericType_View_GenericEnvironment_GenericOptions()
		{
			var factory = new MockFactory();
			var view = new StubView();
			var environment = new StubEnvironment();
			var options = new StubOptions();

			var controller = factory.SpawnController<StubControllerMEA, StubViewModel, StubEnvironment, StubOptions>(
				view,
				environment,
				options);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.ControllerType, Is.EqualTo(typeof(StubControllerMEA)));
			Assert.That(factory.View, Is.SameAs(view));
			Assert.That(factory.ViewModel, Is.TypeOf<StubViewModel>());
			Assert.That(factory.Environment, Is.SameAs(environment));
			Assert.That(factory.Options, Is.SameAs(options));
			Assert.That(factory.ReturnValue, Is.SameAs(controller));
		}

		#endregion

		#region SpawnController<T, M, E, O>(IView, Object, AgentEnvrionment, AgentOptions)

		[Test]
		public void Test_SpawnController_GenericType_View_ViewModel_GenericEnvironment_GenericOptions()
		{
			var factory = new MockFactory();
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();
			var options = new StubOptions();

			var controller = factory.SpawnController<StubControllerMEA, StubViewModel, StubEnvironment, StubOptions>(
				view,
				viewModel,
				environment,
				options);

			Assert.That(factory.Called, Is.EqualTo(1));
			Assert.That(factory.ControllerType, Is.EqualTo(typeof(StubControllerMEA)));
			Assert.That(factory.View, Is.SameAs(view));
			Assert.That(factory.ViewModel, Is.SameAs(viewModel));
			Assert.That(factory.Environment, Is.SameAs(environment));
			Assert.That(factory.Options, Is.SameAs(options));
			Assert.That(factory.ReturnValue, Is.SameAs(controller));
		}

		#endregion

	}
}

#endif
